#include "adc_handle.h"
#include "adc_drv.h"
#include "modbus.h"
#include "adc_table.h"

static void AdcDataEvent(HwName_t hName, uint16_t adValue);
uint16_t Clac_Tempterature(AdcType_R_t type, uint16_t adc_value);
uint8_t GetSwitch(uint16_t adc_value);

/**
*
*/
void ADC_Init(void)
{
    R_ADC_Set_OperationOn();
    AdcDataEventCb(&AdcDataEvent);
  
}

static void AdcDataEvent(HwName_t hName, uint16_t adValue)
{
    int16_t temperature = 0; 
    uint16_t key = 0;
    uint8_t sw1 = 0;
    uint8_t sw2 = 0;
    if(hName == TEMP1)
    {
        temperature = Clac_Tempterature(TEMP1_R, adValue);
        MB_SET(Config_E,E01,temperature);
    }
    else if(hName == TEMP2)
    {
        temperature = Clac_Tempterature(TEMP2_R, adValue);
        MB_SET(Config_E,E08,temperature);
        
    }
    else if(hName == TEMP3)
    {
        temperature = Clac_Tempterature(TEMP3_R, adValue);
        MB_SET(Config_E,E13,temperature);

    }
    else if(hName == TEMP4)
    {
        temperature = Clac_Tempterature(TEMP4_R, adValue);
        MB_SET(Config_E,E14,temperature);

    }
    else if(hName == TEMP5)
    {
        temperature = Clac_Tempterature(TEMP5_R, adValue);
        MB_SET(Config_E,E15,temperature);

        
    }
    else if(hName == TEMP6)
    {
        temperature = Clac_Tempterature(TEMP6_R, adValue);
        MB_SET(Config_E,E16,temperature);

    }
    else if(hName == TEMP7)
    {
        temperature = Clac_Tempterature(TEMP7_R, adValue);
        MB_SET(Config_E,E17,temperature);

    }
    else if(hName == TEMP8)
    {
        temperature = Clac_Tempterature(TEMP8_R, adValue);
        MB_SET(Config_E,E18,temperature);

        
    }
    else if(hName == TEMP9)
    {
        temperature = Clac_Tempterature(TEMP9_R, adValue);
        MB_SET(Config_E,E19,temperature);

    }
    else if(hName == TEMP10)
    {
        temperature = Clac_Tempterature(TEMP10_R, adValue);
        MB_SET(Config_E,E20,temperature);

    }    
    else if(hName == TEMP11)
    {
        temperature = Clac_Tempterature(TEMP11_R, adValue);
        MB_SET(Config_E,E21,temperature);

    }
    else if(hName == TEMP12)
    {
        temperature = Clac_Tempterature(TEMP12_R, adValue);
        MB_SET(Config_E,E22_1,temperature);

    }    
    else if(hName == TEMP13)
    {
        temperature = Clac_Tempterature(TEMP13_R, adValue);
        MB_SET(Config_E,E22_2,temperature);


    }
    else if(hName == TEMP14)
    {
        temperature = Clac_Tempterature(TEMP14_R, adValue);
        MB_SET(Config_E,E23,temperature);

    }
    else if(hName == TEMP15)
    {
        
        temperature = Clac_Tempterature(TEMP15_R, adValue);
        MB_SET(Config_E,E24,temperature);
        
    }
    else if(hName == TEMP16)
    {
        temperature = Clac_Tempterature(TEMP16_R, adValue);
        MB_SET(Config_E,E26,temperature);

    }
    else if(hName == TEMP17)
    {
        temperature = Clac_Tempterature(TEMP17_R, adValue);
        MB_SET(Config_E,E27,temperature);

    }
    else if(hName == TEMP18)
    {
        temperature = Clac_Tempterature(TEMP18_R, adValue);
        MB_SET(Config_E,E28,temperature);
    }
    else if(hName == TEMP19)
    {
        temperature = Clac_Tempterature(TEMP19_R, adValue);
        MB_SET(Config_E,E29,temperature);

    }
    else if(hName == TEMP20)
    {
        temperature = Clac_Tempterature(TEMP20_R, adValue);
        MB_SET(Config_E,E30,temperature);

    }
    else if(hName == TEMP21)
    {
        temperature = Clac_Tempterature(TEMP21_R, adValue);
        MB_SET(Config_E,E31,temperature);

        
    }
    else if(hName == TEMP22)
    {
        temperature = Clac_Tempterature(TEMP22_R, adValue);
        MB_SET(Config_E,E32,temperature);

    }
    else if(hName == TEMP23)
    {
        temperature = Clac_Tempterature(TEMP23_R, adValue);
        MB_SET(Config_E,E33,temperature);

    }
    else if(hName == TEMP24)
    {
        temperature = Clac_Tempterature(TEMP24_R, adValue);
        MB_SET(Config_E,E34,temperature);

        
    }
    else if(hName == TEMP25)
    {
        temperature = Clac_Tempterature(TEMP25_R, adValue);
        MB_SET(Config_E,E35,temperature);

    }
    else if(hName == TEMP26)
    {
        temperature = Clac_Tempterature(TEMP26_R, adValue);
        MB_SET(Config_E,E36,temperature);

    }
    else if(hName == TEMP27)
    {
        temperature = Clac_Tempterature(TEMP27_R, adValue);
        MB_SET(Config_E,E37,temperature);

        
    }
    else if(hName == TEMP28)
    {
        temperature = Clac_Tempterature(TEMP28_R, adValue);
        MB_SET(Config_E,E38,temperature);

    }
    else if(hName == KEY1)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E39,key);
    }
    else if(hName == KEY2)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E40,key);
    }
    else if(hName == KEY3)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E41,key);
    }
    else if(hName == KEY4)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E42,key);
    }
    else if(hName == KEY5)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E43,key);
    }
    else if(hName == KEY6)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E44,key);
    }
    else if(hName == KEY7)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E45,key);
    }
    else if(hName == KEY8)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E46,key);
    }
    else if(hName == KEY9)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E47,key);
    }
    else if(hName == KEY10)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E48,key);
    }
    else if(hName == KEY11)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E49,key);
    }
    else if(hName == KEY12)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E50,key);
    }
    else if(hName == KEY13)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E51,key);
    }
    else if(hName == KEY14)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E52,key);
    }
    else if(hName == KEY15)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E53,key);
    }
    else if(hName == KEY16)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E54,key);
    }
    else if(hName == KEY17)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E55,key);
    }
    else if(hName == KEY18)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E56,key);
    }
    else if(hName == KEY19)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E57,key);
    }
    else if(hName == KEY20)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E58,key);
    }
    else if(hName == KEY21)
    {
        if(adValue > 100)
            key = 1;
        else key = 0;
        MB_SET(Config_E,E59,key);
    }
    else if(hName == SW1)
    {
        sw1 = GetSwitch(adValue);
        MB_SET(Config_E,E60,sw1);
    }
    else if(hName == SW2)
    {
        sw2 = GetSwitch(adValue);
        MB_SET(Config_E,E61,sw2);
    }
}

uint16_t Clac_Tempterature(AdcType_R_t type, uint16_t adc_value)
{
    uint32_t  Rntc;
    uint16_t  temp_tab_num;
    float     temp_dec;
    int16_t   temperature;    	

    switch(type)
    {
        case NTC_R5K:
		    Rntc = (1023 - (uint32_t)adc_value)* _Rd_5 / adc_value;
            temp_tab_num = FindTab(TabNTC_5k, Rntc, TAB_SIZE_5K);
            temp_dec = (TabNTC_5k[temp_tab_num]-Rntc)*1.0/(TabNTC_5k[temp_tab_num]-TabNTC_5k[temp_tab_num+1]);
            temperature = (temp_tab_num - SIZE_IN_ZERO) * 10 + (int16_t)(temp_dec * 10);
			if(temp_tab_num == 0) temperature = (temp_tab_num - SIZE_IN_ZERO) * 10;
			if(temp_tab_num == TAB_SIZE_5K) temperature = (TAB_SIZE_5K - SIZE_IN_ZERO) * 10;
            break;
            
        case NTC_R10K:
		    Rntc = (1023 - (uint32_t)adc_value)* _Rd_5 / adc_value;
            temp_tab_num = FindTab(TabNTC_10k, Rntc, TAB_SIZE_10K);
            temp_dec = (TabNTC_10k[temp_tab_num]-Rntc)*1.0/(TabNTC_10k[temp_tab_num]-TabNTC_10k[temp_tab_num+1]);
            temperature = (temp_tab_num - SIZE_IN_ZERO) * 10 + (int16_t)(temp_dec * 10);
			if(temp_tab_num == 0) temperature = (temp_tab_num - SIZE_IN_ZERO) * 10;
			if(temp_tab_num == TAB_SIZE_10K) temperature = (TAB_SIZE_10K - SIZE_IN_ZERO) * 10;
            break;
            
        case NTC_R20K:
	    	Rntc = (1023 - (uint32_t)adc_value)* _Rd_5 / adc_value;
            temp_tab_num = FindTab(TabNTC_20k, Rntc, TAB_SIZE_20K);
            temp_dec = (TabNTC_20k[temp_tab_num]-Rntc)*1.0/(TabNTC_20k[temp_tab_num]-TabNTC_20k[temp_tab_num+1]);
            temperature = (temp_tab_num - SIZE_IN_ZERO) * 10 + (int16_t)(temp_dec * 10);
			if(temp_tab_num == 0) temperature = (temp_tab_num - SIZE_IN_ZERO) * 10;
			if(temp_tab_num == TAB_SIZE_20K) temperature = (TAB_SIZE_20K - SIZE_IN_ZERO) * 10;
            break;    
            
        case NTC_R10K_D51K:
		    Rntc = (1023 - (uint32_t)adc_value)* _Rd_51 / adc_value;
            temp_tab_num = FindTab(TabNTC_10k, Rntc, TAB_SIZE_10K);
            temp_dec = (TabNTC_10k[temp_tab_num]-Rntc)*1.0/(TabNTC_10k[temp_tab_num]-TabNTC_10k[temp_tab_num+1]);
            temperature = (temp_tab_num - SIZE_IN_ZERO) * 10 + (int16_t)(temp_dec * 10);
			if(temp_tab_num == 0) temperature = (temp_tab_num - SIZE_IN_ZERO) * 10;
			if(temp_tab_num == TAB_SIZE_10K) temperature = (TAB_SIZE_10K - SIZE_IN_ZERO) * 10;
            break;      
            
        case NTC_R10K_D10K:
		    Rntc = (1023 - (uint32_t)adc_value)* _Rd_10 / adc_value;
            temp_tab_num = FindTab(TabNTC_10k, Rntc, TAB_SIZE_10K);
            temp_dec = (TabNTC_10k[temp_tab_num]-Rntc)*1.0/(TabNTC_10k[temp_tab_num]-TabNTC_10k[temp_tab_num+1]);
            temperature = (temp_tab_num - SIZE_IN_ZERO) * 10 + (int16_t)(temp_dec * 10);
			if(temp_tab_num == 0) temperature = (temp_tab_num - SIZE_IN_ZERO) * 10;
			if(temp_tab_num == TAB_SIZE_10K) temperature = (TAB_SIZE_10K - SIZE_IN_ZERO) * 10;
            break;  
            
        case NTC_R50K:
	    	Rntc = (1023 - (uint32_t)adc_value)* _Rd_5 / adc_value;
            temp_tab_num = FindTab(TabNTC_50k, Rntc, TAB_SIZE_50K);
            temp_dec = (TabNTC_50k[temp_tab_num]-Rntc)*1.0/(TabNTC_50k[temp_tab_num]-TabNTC_50k[temp_tab_num+1]);
            temperature = (temp_tab_num - SIZE_IN_ZERO) * 10 + (int16_t)(temp_dec * 10);
			if(temp_tab_num == 0) temperature = (temp_tab_num - SIZE_IN_ZERO) * 10;
			if(temp_tab_num == TAB_SIZE_50K) temperature = (TAB_SIZE_50K - SIZE_IN_ZERO) * 10;
            break;
        case NTC_R100K:
	    	Rntc = (1023 - (uint32_t)adc_value)* _Rd_5 / adc_value;
            temp_tab_num = FindTab(TabNTC_100k, Rntc, TAB_SIZE_100K);
            temp_dec = (TabNTC_100k[temp_tab_num]-Rntc)*1.0/(TabNTC_100k[temp_tab_num]-TabNTC_100k[temp_tab_num+1]);
            temperature = (temp_tab_num - SIZE_IN_ZERO_100K) * 10 + (int16_t)(temp_dec * 10);
			if(temp_tab_num == 0) temperature = (temp_tab_num - SIZE_IN_ZERO_100K) * 10;
			if(temp_tab_num == TAB_SIZE_100K) temperature = (TAB_SIZE_100K - SIZE_IN_ZERO_100K) * 10;
            break;
    }
    return temperature;
}

uint8_t GetSwitch(uint16_t adc_value)
{
    if(SWITCH0_MAX_AD >= adc_value &&  SWITCH1_MAX_AD < adc_value)
        return 0x00;
    else if (SWITCH1_MAX_AD >= adc_value && SWITCH2_MAX_AD <  adc_value)   
        return 0x01;
    else if (SWITCH2_MAX_AD >= adc_value && SWITCH3_MAX_AD <  adc_value)   
        return 0x02;
    else if (SWITCH3_MAX_AD >= adc_value && SWITCH4_MAX_AD <  adc_value) 
        return 0x03;
    else if (SWITCH4_MAX_AD >= adc_value && SWITCH5_MAX_AD <  adc_value)   
        return 0x04;
    else if (SWITCH5_MAX_AD >= adc_value && SWITCH6_MAX_AD <  adc_value)   
        return 0x05;
    else if (SWITCH6_MAX_AD >= adc_value && SWITCH7_MAX_AD <  adc_value) 
        return 0x06;
    else if (SWITCH7_MAX_AD >= adc_value && SWITCH8_MAX_AD <  adc_value)   
        return 0x07;
    else if (SWITCH8_MAX_AD >= adc_value && SWITCH9_MAX_AD <  adc_value)   
        return 0x08;
    else if (SWITCH9_MAX_AD >= adc_value && SWITCHA_MAX_AD <  adc_value) 
        return 0x09;
    else if (SWITCHA_MAX_AD >= adc_value && SWITCHB_MAX_AD <  adc_value)   
        return 0x0a;
    else if (SWITCHB_MAX_AD >= adc_value && SWITCHC_MAX_AD <  adc_value)   
        return 0x0b;
    else if (SWITCHC_MAX_AD >= adc_value && SWITCHD_MAX_AD <  adc_value) 
        return 0x0c;
    else if (SWITCHD_MAX_AD >= adc_value && SWITCHE_MAX_AD <  adc_value)   
        return 0x0d;
    else if (SWITCHE_MAX_AD >= adc_value && SWITCHF_MAX_AD <  adc_value)   
        return 0x0e;
    else if (SWITCHF_MAX_AD >= adc_value) 
        return 0x0f;
    else return 0;
}